# -*- coding: gb2312 -*-

from pymongo import MongoClient
from WindPy import *

import time


from StockDailyFactorGeneralFunctions import StockDailyFactorGeneralFunctions

class StockGeneralData:
    def __init__(self):
        self.baseStartDateStr = '2012-01-01'
        self.baseEndDateStr = '2017-05-01'



    def writeBaseIpoDateToMongoDB(self, colName):

        funcObj = StockDailyFactorGeneralFunctions()
        DBIP = funcObj.DBIP
        DBPort = funcObj.DBPort
        DBName = funcObj.DBName
        endDateStr =self.baseEndDateStr

        resStockID = w.wset('sectorconstituent', 'date=' + endDateStr + ';sectorid=a001010100000000;field=wind_code')

        windStockIdList = resStockID.Data[0]

        ipoDateDictList = []

        allStockIdStr = ''

        client = MongoClient(DBIP, DBPort)
        db = client[DBName]

        collection_ipo_date = db[colName]

        for i in range(0, len(windStockIdList)):
            allStockIdStr = allStockIdStr + str(windStockIdList[i]) + ','

        resIpoDate = w.wss(allStockIdStr, 'ipo_date')

        stockIpoDict = {}
        for i in range(0, len(windStockIdList)):
            aStockIdStr = str(windStockIdList[i])
            aDateStr = resIpoDate.Data[0][i].strftime('%Y%m%d')
            stockIpoDict[aStockIdStr.replace('.', '_')] = aDateStr
        ipoDateDictList.append(stockIpoDict)
        collection_ipo_date.insert(ipoDateDictList)

        print('Finished writing base IPO date to database.')

        self.updateDataUpdateDate('ipo_date_update_date', ' ipo date ', currentDateStr = endDateStr)


    def updateIpoDateToMongoDB(self):

        funcObj = StockDailyFactorGeneralFunctions()
        DBIP = funcObj.DBIP
        DBPort = funcObj.DBPort
        DBName = funcObj.DBName
        colName = 'ipo_date'
        updateDateStr = time.strftime('%Y%m%d')


        resStockID = w.wset('sectorconstituent', 'date=' + updateDateStr + ';sectorid=a001010100000000;field=wind_code')

        windStockIdList = resStockID.Data[0]

        allStockIdStr = ''

        client = MongoClient(DBIP, DBPort)
        db = client[DBName]

        collection_ipo_date = db[colName]

        for i in range(0, len(windStockIdList)):
            allStockIdStr = allStockIdStr + str(windStockIdList[i]) + ','

        resIpoDate = w.wss(allStockIdStr, 'ipo_date')


        for i in range(0, len(windStockIdList)):
            aStockIdStr = str(windStockIdList[i]).replace('.', '_')
            aDateStr = resIpoDate.Data[0][i].strftime('%Y%m%d')
            collection_ipo_date.update({}, {'$set': {aStockIdStr: aDateStr}}, upsert=True)


        print('IPO date for all stocks has been updated...')

        self.updateDataUpdateDate('ipo_date_update_date', ' IPO date for all stocks ')

    def writeBaseTradeStatusFromWindToMongoDB(self):
        baseStartDateStr = self.baseStartDateStr
        baseEndDateStr = self.baseEndDateStr

        windFactorNameStr = 'trade_status'
        colNameStr = 'trade_status'
        dbUpdateDateFiledStr = 'trade_status_update_date'
        dbUpdateDateFieldAliasStr = ' trade status '

        funcObj = StockDailyFactorGeneralFunctions()

        funcObj.writeBaseDataToMongoDB(baseStartDateStr, baseEndDateStr, windFactorNameStr, colNameStr, dbUpdateDateFiledStr, dbUpdateDateFieldAliasStr)

    def writeBaseSwingFromWindToMongoDB(self):
        baseStartDateStr = self.baseStartDateStr
        baseEndDateStr = self.baseEndDateStr

        windFactorNameStr = 'swing'
        colNameStr = 'swing'
        dbUpdateDateFiledStr = 'swing_update_date'
        dbUpdateDateFieldAliasStr = ' swing '

        funcObj = StockDailyFactorGeneralFunctions()

        funcObj.writeBaseDataToMongoDB(baseStartDateStr, baseEndDateStr, windFactorNameStr, colNameStr, dbUpdateDateFiledStr, dbUpdateDateFieldAliasStr)




    ###################################################################################################################
    ### Update Date Data as Current Date
    ###################################################################################################################
    def updateDataUpdateDate(self, strUpdatedKeyName, strUpdatedKeyAlias, currentDateStr = time.strftime('%Y-%m-%d')):
        funcObj = StockDailyFactorGeneralFunctions()
        DBIP = funcObj.DBIP
        DBPort = funcObj.DBPort
        DBName = funcObj.DBName
        colSysData = funcObj.colSysData

        client = MongoClient(DBIP, DBPort)
        db = client[DBName]
        collection_sys_data = db[colSysData]
        try:
            collection_sys_data.update({}, {'$set': {strUpdatedKeyName: currentDateStr}})
        except:
            print('Failed to update  date to MongoDB, please check.......')
            return()
        print('Successfully updated' + strUpdatedKeyAlias + 'into MongoDB.')


    def updateTradeStatusToMongoDB(self):

        windFactorNameStr = 'trade_status'
        colNameStr = 'trade_status'
        dbUpdateDateFiledStr = 'trade_status_update_date'
        dbUpdateDateFieldAliasStr = ' trade status '

        funcObj = StockDailyFactorGeneralFunctions()

        funcObj.updateStringDataToMongoDB(windFactorNameStr, colNameStr, dbUpdateDateFiledStr, dbUpdateDateFieldAliasStr)

    def updateSwingToMongoDB(self):

        windFactorNameStr = 'swing'
        colNameStr = 'swing'
        dbUpdateDateFiledStr = 'swing_update_date'
        dbUpdateDateFieldAliasStr = ' swing '

        funcObj = StockDailyFactorGeneralFunctions()

        funcObj.updateDataToMongoDB(windFactorNameStr, colNameStr, dbUpdateDateFiledStr, dbUpdateDateFieldAliasStr)


obj = StockGeneralData()


baseEndDateStr = '2017-05-01'

#obj.writeBaseIpoDateToMongoDB('ipo_date')
#obj.updateIpoDateToMongoDB()


#obj.writeBaseTradeStatusFromWindToMongoDB()
#obj.updateTradeStatusToMongoDB()

#obj.writeBaseSwingFromWindToMongoDB()
#obj.updateSwingToMongoDB()




